package com.fzk.blog.settings.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Tag;
import com.fzk.blog.bean.vo.TagInfo;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.settings.service.ITagService;
import com.fzk.blog.utils.ResponseResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-17 14:41
 */
@RestController
public class TagController {
    @Resource
    private ITagService tagService;

    @GetMapping(path = "/tagInfo")
    public Map<String, Object> getTagInfoListOrOne(
            @RequestParam(name = "tag_id", required = false) String tagId
    ) throws JsonProcessingException {
        // 1.参数判断
        if (tagId == null) {
            // 2.1 查询列表
            List<TagInfo> tagInfoList = tagService.getTagInfoList();
            return ResponseResult.success("查询成功", tagInfoList);
        } else if (Strings.trimToNull(tagId) == null)
            throw new MyException(400, "参数错误，tag_id：" + tagId);
        else {
            // 2.2 查询特定tagInfo
            TagInfo tagInfo = tagService.getTagInfoById(Integer.parseInt(tagId));
            return ResponseResult.success("查询成功", tagInfo);
        }
    }

    @PostMapping(path = "/tag")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> insertTag(
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) throws JsonProcessingException {
        // 1.参数检查
        checkArgs(name, description);

        // 2.装入参数
        Tag tag = new Tag();
        tag.setName(name);
        tag.setDescription(description);

        // 3.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 4.调用service
        TagInfo tagInfo = tagService.insertTag(tag, loginId);

        // 5.返回结果
        return ResponseResult.success("插入成功", tagInfo);
    }

    @PutMapping(path = "/tag")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> updateTag(
            @RequestParam("tag_id") int tagId,
            @RequestParam("name") String name,
            @RequestParam("description") String description
    ) throws JsonProcessingException {
        // 1.参数检查
        checkArgs(name, description);

        // 2.装入参数
        Tag tag = new Tag();
        tag.setId(tagId);
        tag.setName(name);
        tag.setDescription(description);

        // 3.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 4.调用service
        TagInfo tagInfo = tagService.updateTag(tag, loginId);

        // 5.返回结果
        return ResponseResult.success("修改成功", tagInfo);
    }

    @DeleteMapping(path = "/tag")
    @SaCheckRole("root")//检查角色
    public Map<String, Object> deleteTag(
            @RequestParam("tag_id") int tagId
    ) {
        // 1.取出loginId
        int loginId = Integer.parseInt(StpUtil.getLoginId().toString());

        // 2.调用service
        TagInfo tagInfo = tagService.deleteTag(tagId, loginId);

        // 5.返回结果
        return ResponseResult.success("删除成功", tagInfo);
    }


    /**
     * 参数检查
     *
     * @param args 可变形参
     */
    private void checkArgs(String... args) {
        for (String arg : args) {
            if (arg == null || arg.length() == 0)
                throw new MyException(400, "参数错误：" + arg);
        }
    }
}
